package cn.iocoder.yudao.framework.common.util.area;

import java.util.Locale;

/**
 * Description:
 * Author: li
 * Date: 2024/4/3
 */
public class CountryUtil {

    /**
     * 通过国家名称获取国家代码
     *
     * @param countryName
     * @return
     */
    public static String getCountryCode(String countryName) {
        Locale locale = new Locale("", "");
        String[] countryCodes = Locale.getISOCountries();

        for (String code : countryCodes) {
            Locale loc = new Locale("", code);
            if (countryName.equals(loc.getDisplayCountry(locale))) {
                return code;
            }
        }

        return null;
    }

    public static void main(String[] args) {
        String countryName = "China";
        String countryCode = getCountryCode(countryName);
        System.out.println(countryCode);
    }

}
